package com.wuzhishan.news.controller;

import com.wuzhishan.news.pojo.BlogArticle;
import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.service.Impl.articleServiceImpl;
import com.wuzhishan.news.utils.numberTransUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.ParseException;

@Controller
public class ArticleController {
    @Autowired
    private articleServiceImpl articleService;

    /**
     * 跳转到文章发布页面
     * @return
     */
    @RequestMapping("/post/new")
    public Object goAddNews(HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        return new ModelAndView("/post/news");
    }

    /**
     * 提交文章
     * @return
     */
    @RequestMapping("/post/submit")
    public String AddNews(@RequestParam("title") String title,
                          @RequestParam("group") String group,
                          @RequestParam("content") String content,
                          HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user == null){
            //用户未登录，返回首页
            return "redirect:/login.html";
        }
        articleService.addArticle(title,group,content,user);
        return "redirect:/home";
    }

    /**
     * 文章详情页面
     * @return
     */
    @RequestMapping(value = {"/view/{artId}.html","/view/{artId}"})
    public Object AddNews(@PathVariable String artId,
                          ModelAndView mv){
        System.out.println(artId);
        String articleId = numberTransUtil.TransNum(artId);
        mv = articleService.getArticleById(articleId,mv);
        //点击文章详情页，文章的阅读量加一
        articleService.hitPlus(articleId);
        mv.setViewName("/article/view");
        return mv;
    }
}
